﻿#ifndef _FACE_MESSAGE_V1_H_
#define _FACE_MESSAGE_V1_H_

#define FACE_USER_NAME_SIZE 32 //用户名大小.
#define FACE_VERSION_INFO_BUFFER_SIZE 32 //版本信息大小.
#define FACE_MAX_USER_COUNTS 50 //最大用户数量.

//通信消息ID定义.
const unsigned char MID_REPLY = 0x00; //模块到主机:请求(命令)回复消息，成功与数据或失败与reason.
const unsigned char MID_NOTE  = 0x01; //模块到主机:注意到主机例如位置或角度.
const unsigned char MID_IMAGE = 0x02; //模块到主机:发送图像到主机.

const unsigned char MID_RESET               = 0x10; //复位.
const unsigned char MID_GET_STATUS          = 0x11; //获取状态.
const unsigned char MID_VERIFY              = 0x12; //鉴权解锁.
const unsigned char MID_ENROLL              = 0x13; //交互式注册.
const unsigned char MID_ENROLL_SINGLE       = 0x1D; //单帧注册.
const unsigned char MID_ENROLL_ITG          = 0x26; //集成支持并扩展所有注册方式.
const unsigned char MID_SNAP_IMAGE          = 0x16; //拍照并保存.
const unsigned char MID_GET_SAVED_IMAGE     = 0x17; //获取保存图像的大小.
const unsigned char MID_UPLOAD_IMAGE        = 0x18; //上传图像.
const unsigned char MID_DELETE_USER         = 0x20; //删除指定用户，用户id.
const unsigned char MID_DELETE_ALL          = 0x21; //删除所有已注册的用户.
const unsigned char MID_GET_USER_INFO       = 0x22; //获取用户信息.
const unsigned char MID_FACE_RESET          = 0x23; //重置用户状态.
const unsigned char MID_GET_ALL_USERID      = 0x24; //获取所有用户ID.
const unsigned char MID_GET_VERSION         = 0x30; //获取版本信息.
const unsigned char MID_START_OTA           = 0x40; //要求模块退出OTA模式.
const unsigned char MID_STOP_OTA            = 0x41; //要求模块退出OTA模式.
const unsigned char MID_GET_OTA_STATUS      = 0x42; //查询当前ota状态.
const unsigned char MID_OTA_HEADER          = 0x43; //OTA首个数据包.
const unsigned char MID_OTA_PACKET          = 0x44; //OTA数据包，携带真实固件数据.
const unsigned char MID_INIT_ENCRYPTION     = 0x50; //初始化加密通信.
const unsigned char MID_CONFIG_BAUDRATE     = 0x51; //配置串口波特率.
const unsigned char MID_SET_RELEASE_ENC_KEY = 0x52; //设置发布加密密钥，警告:一旦设置, 密钥将不可更改.
const unsigned char MID_SET_DEBUG_ENC_KEY   = 0x53; //设置调试加密的key.
const unsigned char MID_GET_LOGFILE         = 0x60; //获取日志文件.
const unsigned char MID_UPLOAD_LOGFILE      = 0x61; //上传日志文件.
const unsigned char MID_POWERDOWN           = 0xED; //下电.
const unsigned char MID_DEBUG_MODE          = 0xF0; //进入debug模式.
const unsigned char MID_GET_DEBUG_INFO      = 0xF1; //获取debug信息.
const unsigned char MID_UPLOAD_DEBUG_INFO   = 0xF2; //上传debug.
const unsigned char MID_GET_LIBRARY_VERSION = 0xF3; //获取库版本.
const unsigned char MID_DEMO_MODE           = 0xFE; //进入演示模式，验证流程将跳过特征比较步骤.
const unsigned char MID_MAX                 = 0xFF; //保留.

//消息结果码result码.
const unsigned char MR_SUCCESS               = 0;  //成功.
const unsigned char MR_FAILED_REJECTED       = 1;  //模块拒绝此命令.
const unsigned char MR_FAILED_ABORTED        = 2;  //算法中断.
const unsigned char MR_FAILED_CAMERA         = 4;  //相机打开失败.
const unsigned char MR_FAILED_UNKNOWN_REASON = 5;  //未知错误.
const unsigned char MR_FAILED_INVALID_PARAM  = 6;  //非法参数.
const unsigned char MR_FAILED_NO_MEMORY      = 7;  //内存溢出.
const unsigned char MR_FAILED_UNKNOWN_USER   = 8;  //未注册的用户.
const unsigned char MR_FAILED_MAXUSER        = 9;  //用户ID超限.
const unsigned char MR_FAILED_FACE_ENROLLED  = 10; //人脸已注册.
const unsigned char MR_FAILED_LIVENESS_CHECK = 12; //活体检测失败.
const unsigned char MR_FAILED_TIMEOUT        = 13; //操作超时.
const unsigned char MR_FAILED_AUTHORIZATION  = 14; //授权失败.
const unsigned char MR_FAILED_CAMERA_FOV     = 15; //相机焦距测试失败.
const unsigned char MR_FAILED_CAMERA_QUA     = 16; //相机质量测试失败.
const unsigned char MR_FAILED_CAMERA_STRU    = 17; //摄像机结构测试失败.
const unsigned char MR_FAILED_BOOT_TIMEOUT   = 18; // boot启动超时.
const unsigned char MR_FAILED_READ_FILE      = 19; //读文件失败.
const unsigned char MR_FAILED_WRITE_FILE     = 20; //写文件失败.
const unsigned char MR_FAILED_NO_ENCRYPT     = 21; //必须设置加密.
const unsigned char MR_FAILED_NO_RGBIMAGE    = 23; //rgb图像没有准备好.

//模块状态state.
const unsigned char MS_STANDBY = 0; //空闲，等待指令中.
const unsigned char MS_BUSY    = 1; //繁忙中，正在工作或处理指令.
const unsigned char MS_ERROR   = 2; //出错状态，可能不会工作.
const unsigned char MS_INVALID = 3; //未初始化.
const unsigned char MS_OTA     = 4; //处于OTA状态.

//人脸方向，可以多项拼接使用.
const unsigned char FACE_DIRECTION_UP       = 0x10; //人脸朝上.
const unsigned char FACE_DIRECTION_DOWN     = 0x08; //人脸朝下.
const unsigned char FACE_DIRECTION_LEFT     = 0x04; //人脸朝左.
const unsigned char FACE_DIRECTION_RIGHT    = 0x02; //人脸朝右.
const unsigned char FACE_DIRECTION_MIDDLE   = 0x01; //人脸正向.
const unsigned char FACE_DIRECTION_UNDEFINE = 0x00; //未定义方向.

//提示类消息ID.
const unsigned char NOTE_ID_READY = 0; //模块处理请求命令就绪.
const unsigned char NOTE_ID_FACE_STATE    = 1; //检测到人脸状态描述.
const unsigned char NOTE_ID_UNKNOWN_ERROR = 2; //未知错误.
const unsigned char NOTE_ID_OTA_DONE      = 3; //OTA升级程序结束.
const unsigned char NOTE_ID_EYE_STATE     = 4; //人脸处于闭眼状态.

//人脸注册类型.
const unsigned char FACE_ENROLL_TYPE_INTERACTIVE = 0; //交互式注册.
const unsigned char FACE_ENROLL_TYPE_SINGLE      = 1; //单帧图像注册.

//人脸状态，用于提示用户.
const unsigned char FACE_STATE_NORMAL    = 0; //人脸正常.
const unsigned char FACE_STATE_NOFACE    = 1; //未检测到人脸.
const unsigned char FACE_STATE_TOO_UP    = 2; //人脸太靠近图片上边沿.
const unsigned char FACE_STATE_TOO_DOWN  = 3; //人脸太靠近图片下边沿.
const unsigned char FACE_STATE_TOO_LEFT  = 4; //人脸太靠近图片左边沿.
const unsigned char FACE_STATE_TOO_RIGHT = 5; //人脸太靠近图片右边沿.
const unsigned char FACE_STATE_TOO_FAR   = 6; //人脸距离太远.
const unsigned char FACE_STATE_TOO_CLOSE = 7; //人脸距离太近.
const unsigned char FACE_STATE_EYEBROW_OCCLUSION = 8; //眉毛遮挡.
const unsigned char FACE_STATE_EYE_OCCLUSION     = 9; //眼睛遮挡.
const unsigned char FACE_STATE_FACE_OCCLUSION    = 10; //人脸遮挡.
const unsigned char FACE_STATE_DIRECTION_ERROR   = 11; //注册人脸方向错误.
const unsigned char FACE_STATE_EYE_CLOSE_STATUS_OPEN_EYE = 12; //在闭眼模式检测到睁眼状态.
const unsigned char FACE_STATE_EYE_CLOSE_STATUS          = 13; //确认闭眼状态.
const unsigned char FACE_STATE_EYE_CLOSE_UNKNOW_STATUS   = 14; //在闭眼模式检测中无法判定睁闭眼状态.

const unsigned char LOG_FILE_KERNEL        = 0; //内核日志文件.
const unsigned char LOG_FILE_SENSELOCK_APP = 1; //Senselock应用程序日志文件.
const unsigned char LOG_FILE_FACE_MODULE   = 2; //人脸模块日志文件.

//enc key type.
const unsigned char RELEASE_ENC_KEY = 0; // for release
const unsigned char DEBUG_ENC_KEY   = 1; // for debug

//通用消息结构，接收消息分2次，第一次接收消息头，第2次接收具体消息数据.
//MID指令组成：s_msg_head + s_msg_XXX + checksum
//REPLY应答指令组成：s_msg_head + s_reply_head + checksum
//NOTE应答指令组成：s_msg_head + s_note_head + checksum
//IMAGE应答指令组成：s_msg_head + image_data + checksum

struct s_msg_head{
    unsigned char head[2]; //同步头，固定未EFH,AAH.
    unsigned char mid;     //消息ID，参照REPLY:0x00;NOTE:0x01;IMAGE:0x02.
    unsigned char len[2];  //高8bit.
};

//鉴权解锁 MID_VERIFY
struct s_msg_verify{
    unsigned char pd_rightaway; //认证后立即断电.
    unsigned char timeout; //超时时间，单位秒，默认10s.
};

//注册人脸 MID_ENROLL . MID_ENROLL_SINGLE.
struct s_msg_enroll{
    unsigned char admin;          //管理员标记：1:管理员，0:普通用户.
    unsigned char user_name[FACE_USER_NAME_SIZE];  //用户名称.
    unsigned char face_direction; //人脸方向.
    unsigned char timeout;        //超时时间，单位秒，默认10s.
};

//交互式注册人脸 MID_ENROLL_ITG
struct s_msg_enroll_itg{
    unsigned char admin;            //管理员标记：1:管理员，0:普通用户.
    unsigned char user_name[FACE_USER_NAME_SIZE];    //用户名称.
    unsigned char face_direction;   //参照 FACE_DIRECTION_*
    unsigned char enroll_type;      //参照 FACE_ENROLL_TYPE_*
    unsigned char enable_duplicate; //允许用户重复注册, 1:使能，0:禁止.
    unsigned char reserved[3];      //保留.
};

//删除用户 MID_DELUSER
struct s_msg_delete_user{
    unsigned char user_id_heb; //用户ID高8bit.
    unsigned char user_id_leb; //用户ID低8bit.
};

//获取用户信息 MID_GETUSERINFO
struct s_msg_get_user_info{
    unsigned char user_id_heb; //用户ID高8bit.
    unsigned char user_id_leb; //用户ID低8bit.
};

//开始OTA功能 MID_START_OTA
struct s_msg_start_ota{
    unsigned char v_primary;   //版本号1.
    unsigned char v_secondary; //版本号2.
    unsigned char v_revision;  //版本号.
    unsigned char ota_type;    //OTA分类: 0:OS, 1:facelib
};

//OTA首包 MID_OTA_HEADER
struct s_msg_ota_header{
    unsigned char fsize_b[4]; // OTA FW file size int-> [b1, b2, b3, b4]
    unsigned char num_pkt[4]; //要分割传送的包数, int->[b1, b2, b3, b4]
    unsigned char pkt_size[2]; //单包数据大小.
    unsigned char md5_sum[32]; //MD5校验码.
};

//OTA包结构 MID_OTA_PACKET
struct s_msg_ota_packet{
    unsigned char pid[2];   //包序号.
    unsigned char psize[2]; //包大小.
    unsigned char data[0];  //数据开始.
};

//初始化加密通信 MID_INIT_ENCRYPTION
struct s_msg_init_encryption{
    unsigned char seed[4]; //随机数据作为种子.
    unsigned char mode;    //为选择加密模式保留.
    unsigned char crttime[4]; //当前时间-这是模块与主机同步时间的接口.
};

// demo data
struct s_msg_demo_mode{
    unsigned char enable; //使能demo或者禁能.
};

// debug mode data
struct s_msg_debug_mode{
    unsigned char enable; //使能debug模式或者禁能.
};

// snap image data
struct s_msg_snap_image{
    unsigned char image_counts; //图像数量.
    unsigned char start_number; //起始图像编号.
};

// MID_GET_SAVED_IMAGE
struct s_msg_get_saved_image{
    unsigned char image_number; //图像编号.
};

// upload image
struct s_msg_upload_image{
    unsigned char upload_image_offset[4]; // upload image offset, int
    unsigned char upload_image_size[4]; // upload image size, int
};

// upload debug informaiton
struct s_msg_upload_debug_info{
    unsigned char upload_debug_info_offset[4]; // upload debug informaiton offset, int -> [o1 o2 o3 o4]
    unsigned char upload_debug_info_size[4]; // upload debug informaiton size, int->[s1 s2 s3 s4]
};

// upload logfile
struct s_msg_upload_logfile{
    unsigned char upload_logfile_offset[4];// upload logfile offset, int -> [o1 o2 o3 o4]
    unsigned char upload_logfile_size[4]; // uploade logfile size, int -> [o1 o2 o3 o4]
};

// DDR test
struct s_msg_ddr_test{
    unsigned char ddr_test_counts; //DDR编号.
};

struct s_msg_enc_key_number{
    unsigned char enc_key_number[16];
};

struct s_reply_head{
    unsigned char mid; // the command(message) id to reply (the request message ID)
    unsigned char result; // command handling result: success or failed->	msg_result_t
    unsigned char data[0];
};

//message reply data definitions
struct s_reply_get_status{
    unsigned char status; //IDLE(0),BUSY(1),ERROR(2),INVALID(3)
};

struct s_reply_verify{
    unsigned char user_id_heb;  //用户ID高8bit.
    unsigned char user_id_leb;  //用户ID低8bit.
    unsigned char user_name[32]; //用户名称.
    unsigned char admin;
    unsigned char unlockStatus; //解锁过程中睁闭眼状态
};

struct s_reply_enroll{
    unsigned char user_id_heb;  //用户ID高8bit.
    unsigned char user_id_leb;  //用户ID低8bit.
    unsigned char face_direction; //人脸方向.
};

struct s_reply_get_version{
    unsigned char version_info[FACE_VERSION_INFO_BUFFER_SIZE];
};

struct s_reply_get_library_version{
    unsigned char library_version_info[20];
};

struct s_reply_get_user_info{
    unsigned char user_id_heb;
    unsigned char user_id_leb;
    unsigned char user_name[32];
    unsigned char admin;
};

struct s_reply_all_userid{
    unsigned char user_counts; // number of enrolled users
    unsigned char users_id[FACE_MAX_USER_COUNTS*2]; //use 2 bytes to save a userID and save high eight bits firstly
};

struct s_reply_factory_test{
    unsigned char time_heb; // high eight bits of factory test time
    unsigned char time_leb; // low eight bits of facetory
};
// MID_INIT_ENCRYPTION

struct s_reply_init_encryption{
    unsigned char device_id[20]; // the unique ID of this device, string type
};
// REPLY MID_GET_OTA_STATUS

struct s_reply_get_ota_status{
    unsigned char ota_status; // current ota status
    unsigned char next_pid_e[2]; // expected next pid,[b0,b1]
};

struct s_reply_get_saved_image{
    unsigned char image_size[4]; // image size int
} ;

struct s_reply_get_debug_info{
    unsigned char debug_file_size[4]; // debug file size int->[s1, s2, s3, s4]
};

//NOTE消息结构.
struct s_note_head{
    unsigned char nid; //note id
    unsigned char data[0]; //
};

//人脸提示数据.
struct s_note_data_face{
    unsigned short state;  //人脸状态，对应到FACE_STATE_
    unsigned short left;   //靠左像素.
    unsigned short top;    //靠上像素.
    unsigned short right;  //靠右像素.
    unsigned short bottom; //靠下像素.
    unsigned short yaw;    //偏航，摇头.
    unsigned short pitch;  //俯仰，点头.
    unsigned short roll;   //滚转，摆头.
};

struct s_note_ota_result{
    unsigned char ota_result; //0:sucess; 1:faile;
};

struct s_note_config_baudrate{
    unsigned char baudrate_index; //1:115200; 2:230400; 3:460800; 4:1500000(n*115200*1);
};

#endif // _FACE_MESSAGE_V1_H_
